package cn.edu.hunau.oop.day07;

import java.util.ArrayList;
import java.util.List;
import java.util.function.Function;
import java.util.function.Predicate;

/**
 * @author hb
 * @date 2025-08-12 10:31:26
 */
public class FilterApple {

    public static <R> List<R> map(List<Apple> datas, Function<Apple,R> fun){
        List<R> result=new ArrayList<>();
        for(Apple apple:datas){
            result.add(fun.apply(apple));
        }
        return result;
    }

    public static List<Apple>  filter(List<Apple> datas, Predicate<Apple> filter){
        List<Apple> result=new ArrayList<>();
        for(Apple apple:datas){
            if(filter.test(apple)){
                result.add(apple);
            }
        }
        return result;
    }

/*    public static List<Apple>  filter(List<Apple> datas,Filter filter){
        List<Apple> result=new ArrayList<>();
        for(Apple apple:datas){
            if(filter.filter(apple)){
                result.add(apple);
            }
        }
        return result;
    }*/

    /**
     * 定义根据颜色过滤的方法对数据进行过滤
     * @param datas
     * @param color
     * @return
     */
    public static List<Apple> filterByColor(List<Apple> datas,String color){
        List<Apple> result=new ArrayList<>();
        for(Apple apple:datas){
            if(color.equals(apple.getColor())){
                result.add(apple);
            }
        }

        return result;
    }

    public static List<Apple> filterByWeight(List<Apple> datas,double weight){
        List<Apple> result=new ArrayList<>();
        for(Apple apple:datas){
            if(weight>=apple.getWeight()){
                result.add(apple);
            }
        }
        return result;
    }

    public static List<Apple> filterByType(List<Apple> datas,String type){
        List<Apple> result=new ArrayList<>();
        for(Apple apple:datas){
            if(type.equals(apple.getType())){
                result.add(apple);
            }
        }
        return result;
    }
}
